
'use strict';

const moment = require('moment');

// 格式化时间
exports.formatTime = time => moment(time).format('YYYY-MM-DD HH:mm:ss');

// 处理成功响应
exports.success = ({ ctx, res = null, msg = '请求成功' }) => {
  ctx.body = {
    code: 0,
    data: res,
    msg,
  };
  ctx.status = 200;
};

exports.failure = ({ ctx, err = null, msg = '参数错误' }) => {
  ctx.body = {
    code: 0,
    data: err,
    msg,
  };
  ctx.status = 422;
};

exports.token = (ctx, userId) => {
	return ctx.app.jwt.sign({
      data: {
        _id: userId
      },
      exp: Math.floor(Date.now() / 1000) + (3600 * 24 * 7)
    }, ctx.app.config.jwt.secret);
}
